/*
 * Tower.cpp
 *
 *  Created on: Oct 16, 2010
 *      Author: Jacob
 */
#pragma once
#include "stdafx.h"
#include "Tower.h"
#include <iostream>
using std::cout;

Tower* Tower::newTowerWithName(string name)
{
	return new Tower(name);
}

Tower* Tower::newTowerWithStartingPegsAndName(int numberOfPegs, string name)
{
	return new Tower(numberOfPegs, name);
}

Tower::Tower(string name)
{
	this->name = name;
	this->pegs = Stack<Peg*>::newStack();
}

Tower::Tower(int numberOfPegs,string name)
{
	this->name = name;
	pegs = Stack<Peg*>::newStack();
	generatePegs(numberOfPegs);
}

Tower::~Tower()
{
	cout << "\n" << this->name << " Destructor Called";
	delete pegs;
}

bool Tower::addPeg(Peg* pegToBeAdded)
{
	if (isEmpty() == true)
	{
		pegs->push(pegToBeAdded);
		return true;
	}
	else
	{
		if (pegCanBeTaken(pegToBeAdded))
		{
			pegs->push(pegToBeAdded);
			return true;
		}
		else
			return false;
	}
}

bool Tower::isEmpty()
{
	if (pegs->getSize() <= 0)
		return true;
	else
		return false;
}

void Tower::generatePegs(int numberOfPegs)
{
	for (int index = numberOfPegs; index > 0; index--)
	{
		Peg* newPeg = Peg::createPegWithSize(index);
		pegs->push(newPeg);
	}
}

bool Tower::pegCanBeTaken(Peg* potentialPeg)
{
	if (pegs->getTop()->getData()->getSize() < potentialPeg->getSize())
		return false;
	else
		return true;
}

Peg* Tower::removeTopPeg()
{
	return pegs->pop()->getData();
}

void Tower::clearTower()
{
	pegs->clear();
}

bool Tower::hasPegs()
{
	if(pegs->getSize() > 0)
	{
		return true;
	}
	else
		return false;
}

void Tower::setName(string name)
{
	this->name = name;
}

string Tower::toString()
{
	ostringstream stringStream;
	stringStream << "\n" << name;
	stringStream << pegs->toString();
	return stringStream.str();
}

